package com.kl.common.util;

import com.kl.common.encrypt.Base64;
import com.kl.common.encrypt.MD5;
import org.apache.commons.lang3.StringUtils;

/**
 * 密码加密解密
 * @author XRP_ZJB
 * @date 2016/1/15 16:08
 *
 */
public class EncryptionUtil {
	
	/**
	 * 加密
	 * @param password
	 * @return
	 */
	public static String encrypt(String password , String key){
		// 加密
		String basePwd = Base64.encrypt(password.getBytes());
		basePwd = MD5.encrypt(MD5.encrypt(basePwd) + key);
		return basePwd;
	}
	
	/**
	 * 解密
	 * @param inputPwd
	 * @param salt
	 * @return
	 */
	public static Boolean decrypt(String inputPwd, String salt, String dataPwd) {
		String inputEncrypt = encrypt(inputPwd, salt);
		if (!StringUtils.isEmpty(inputEncrypt) && StringUtils.equals(inputEncrypt, dataPwd)) {
			return true;
		}
		return false;
	}
	
	/**
	 * 随机生成KEY
	 * @return
	 */
	public static String getKey(){
		String key = UUIDUtil.getUniquekey();
		return Base64.encrypt(key.getBytes());
	}
	
	public static void main(String[] args) {
		String key = getKey();
		String basePwd = encrypt("1234561321313", key);
		System.out.println("加密后密文：" + basePwd + " key：" + key);
		System.out.println(MD5.encrypt("123456"));
		
	}
	
}
